Skip to contentSkip to conversation

Change in openvpn[master]: DNS/DHCP: Make _len attributes consistently unsigned

Openvpn-devel In review flichtenheld (Code Review) 2 participants 6 messages Feb 12 – Mar 12 1 message not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): gerrit.1770918401000.Id2c5df9df32a02e944e13b55f57a2c1928b652f4@...2715...

#1 lore raw

Attention is currently required from: plaisthos.

Hello plaisthos,

I'd like you to do a code review.
Please visit

    http://gerrit.openvpn.net/c/openvpn/+/1520?usp=email

to review the following change.

Change subject: DNS/DHCP: Make _len attributes consistently unsigned
......................................................................

DNS/DHCP: Make _len attributes consistently unsigned

Use unsigned int instead of size_t since 32bit
values are quite enough here and this avoids some
unneccesary casts.

Change-Id Id2c5df9df32a02e944e13b55f57a2c1928b652f4

---
M include/openvpn-msg.h
M src/openvpn/dns.h
M src/openvpn/options.c
M src/openvpn/tun.c
M src/openvpn/tun.h
M src/openvpnserv/interactive.c

6 files changed, 62 insertions(+), 78 deletions(-)

git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/20/1520/1

include/openvpn-msg.h +2 -2
index 34f50c8..bef20cf 100644
@@ -103,7 +103,7 @@
     interface_t iface;
     char domains[512];
     short family;
-    int addr_len;
+    unsigned int addr_len;
     inet_address_t addr[4]; /* support up to 4 dns addresses */
 } dns_cfg_message_t;
 
@@ -130,7 +130,7 @@
 {
     message_header_t header;
     interface_t iface;
-    int addr_len;
+    unsigned int addr_len;
     inet_address_t addr[4]; /* support up to 4 dns addresses */
 } wins_cfg_message_t;
 
src/openvpn/dns.h +3 -3
index 882e474..51bc2de 100644
@@ -99,14 +99,14 @@
 struct dhcp_options
 {
     in_addr_t dns[N_DHCP_ADDR];
-    int dns_len;
+    unsigned int dns_len;
 
     struct in6_addr dns6[N_DHCP_ADDR];
-    int dns6_len;
+    unsigned int dns6_len;
 
     const char *domain;
     const char *domain_search_list[N_SEARCH_LIST_LEN];
-    int domain_search_list_len;
+    unsigned int domain_search_list_len;
 };
 
 struct dns_options
src/openvpn/options.c +13 -15
index 6bebeb7..ddd1165 100644
@@ -1271,23 +1271,21 @@
 #ifndef ENABLE_SMALL
 
 static void
-show_dhcp_option_list(const char *name, const char *const *array, int len)
+show_dhcp_option_list(const char *name, const char *const *array, unsigned int len)
 {
-    int i;
-    for (i = 0; i < len; ++i)
+    for (unsigned int i = 0; i < len; ++i)
     {
-        msg(D_SHOW_PARMS, "  %s[%d] = %s", name, i, array[i]);
+        msg(D_SHOW_PARMS, "  %s[%u] = %s", name, i, array[i]);
     }
 }
 
 static void
-show_dhcp_option_addrs(const char *name, const in_addr_t *array, int len)
+show_dhcp_option_addrs(const char *name, const in_addr_t *array, unsigned int len)
 {
     struct gc_arena gc = gc_new();
-    int i;
-    for (i = 0; i < len; ++i)
+    for (unsigned int i = 0; i < len; ++i)
     {
-        msg(D_SHOW_PARMS, "  %s[%d] = %s", name, i, print_in_addr_t(array[i], 0, &gc));
+        msg(D_SHOW_PARMS, "  %s[%u] = %s", name, i, print_in_addr_t(array[i], 0, &gc));
     }
     gc_free(&gc);
 }
@@ -1319,12 +1317,12 @@
 #endif /* ifdef _WIN32 */
 
 static void
-dhcp_option_dns6_parse(const char *parm, struct in6_addr *dns6_list, int *len, msglvl_t msglevel)
+dhcp_option_dns6_parse(const char *parm, struct in6_addr *dns6_list, unsigned int *len, msglvl_t msglevel)
 {
     struct in6_addr addr;
     if (*len >= N_DHCP_ADDR)
     {
-        msg(msglevel, "--dhcp-option DNS: maximum of %d IPv6 dns servers can be specified",
+        msg(msglevel, "--dhcp-option DNS: maximum of %u IPv6 dns servers can be specified",
             N_DHCP_ADDR);
     }
     else if (get_ipv6_addr(parm, &addr, NULL, msglevel))
@@ -1333,12 +1331,12 @@
     }
 }
 static void
-dhcp_option_address_parse(const char *name, const char *parm, in_addr_t *array, int *len,
+dhcp_option_address_parse(const char *name, const char *parm, in_addr_t *array, unsigned int *len,
                           msglvl_t msglevel)
 {
     if (*len >= N_DHCP_ADDR)
     {
-        msg(msglevel, "--dhcp-option %s: maximum of %d %s servers can be specified", name,
+        msg(msglevel, "--dhcp-option %s: maximum of %u %s servers can be specified", name,
             N_DHCP_ADDR, name);
     }
     else
@@ -3664,7 +3662,7 @@
             new->name = dhcp->domain;
             entry = &new->next;
 
-            for (int i = 0; i < dhcp->domain_search_list_len; ++i)
+            for (unsigned int i = 0; i < dhcp->domain_search_list_len; ++i)
             {
                 ALLOC_OBJ_CLEAR_GC(*entry, struct dns_domain, &dns->gc);
                 struct dns_domain *new = *entry;
@@ -3674,13 +3672,13 @@
 
             struct dns_server *server = dns_server_get(&dns->servers, 0, &dns->gc);
             const size_t max_addrs = SIZE(server->addr);
-            for (int i = 0; i < dhcp->dns_len && server->addr_count < max_addrs; ++i)
+            for (unsigned int i = 0; i < dhcp->dns_len && server->addr_count < max_addrs; ++i)
             {
                 server->addr[server->addr_count].in.a4.s_addr = htonl(dhcp->dns[i]);
                 server->addr[server->addr_count].family = AF_INET;
                 server->addr_count += 1;
             }
-            for (int i = 0; i < dhcp->dns6_len && server->addr_count < max_addrs; ++i)
+            for (unsigned int i = 0; i < dhcp->dns6_len && server->addr_count < max_addrs; ++i)
             {
                 server->addr[server->addr_count].in.a6 = dhcp->dns6[i];
                 server->addr[server->addr_count].family = AF_INET6;
src/openvpn/tun.c +32 -42

204 lines not on this page · raw message

src/openvpn/tun.h +6 -6
index 388f5b7..962a079 100644
@@ -109,25 +109,25 @@
 
     /* DNS (6) */
     in_addr_t dns[N_DHCP_ADDR];
-    int dns_len;
+    unsigned int dns_len;
 
     /* WINS (44) */
     in_addr_t wins[N_DHCP_ADDR];
-    int wins_len;
+    unsigned int wins_len;
 
     /* NTP (42) */
     in_addr_t ntp[N_DHCP_ADDR];
-    int ntp_len;
+    unsigned int ntp_len;
 
     /* NBDD (45) */
     in_addr_t nbdd[N_DHCP_ADDR];
-    int nbdd_len;
+    unsigned int nbdd_len;
 
 #define N_SEARCH_LIST_LEN 10 /* Max # of entries in domin-search list */
 
     /* SEARCH (119), MacOS, Linux, Win10 1809+ */
     const char *domain_search_list[N_SEARCH_LIST_LEN];
-    int domain_search_list_len;
+    unsigned int domain_search_list_len;
 
     /* DISABLE_NBT (43, Vendor option 001) */
     bool disable_nbt;
@@ -138,7 +138,7 @@
     bool register_dns;
 
     struct in6_addr dns6[N_DHCP_ADDR];
-    int dns6_len;
+    unsigned int dns6_len;
 #if defined(TARGET_ANDROID)
     const char *http_proxy;
     int http_proxy_port;
src/openvpnserv/interactive.c +6 -10
index b50aeb3..c96d3f2 100644
@@ -1814,10 +1814,10 @@
 {
     DWORD err = 0;
     undo_type_t undo_type = (msg->family == AF_INET6) ? undo_dns6 : undo_dns4;
-    int addr_len = msg->addr_len;
+    unsigned int addr_len = msg->addr_len;
 
     /* sanity check */
-    const int max_addrs = _countof(msg->addr);
+    const unsigned int max_addrs = _countof(msg->addr);
     if (addr_len > max_addrs)
     {
         addr_len = max_addrs;
@@ -1875,7 +1875,7 @@
          * time constant by all compilers and constexpr is C23 */
         CHAR addrs[_countof(msg->addr) * 64]; /* 64 is enough for one IPv4/6 address */
         size_t offset = 0;
-        for (int i = 0; i < addr_len; ++i)
+        for (unsigned int i = 0; i < addr_len; ++i)
         {
             if (i != 0)
             {
@@ -2936,14 +2936,10 @@
 {
     DWORD err = NO_ERROR;
     wchar_t addr[16]; /* large enough to hold string representation of an ipv4 */
-    int addr_len = msg->addr_len;
+    unsigned int addr_len = msg->addr_len;
 
     /* sanity check */
-    if (addr_len < 0)
-    {
-        addr_len = 0;
-    }
-    if ((unsigned int)addr_len > _countof(msg->addr))
+    if (addr_len > _countof(msg->addr))
     {
         addr_len = _countof(msg->addr);
     }
@@ -2971,7 +2967,7 @@
         goto out; /* job done */
     }
 
-    for (int i = 0; i < addr_len; ++i)
+    for (unsigned int i = 0; i < addr_len; ++i)
     {
         RtlIpv4AddressToStringW(&msg->addr[i].ipv4, addr);
         err = netsh_wins_cmd(i == 0 ? L"set" : L"add", msg->iface.index, addr);

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/1520?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings?usp=email

Gerrit-MessageType: newchange
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Id2c5df9df32a02e944e13b55f57a2c1928b652f4
Gerrit-Change-Number: 1520
Gerrit-PatchSet: 1
Gerrit-Owner: flichtenheld <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>

#2 lore raw
In reply to a message not in the archive

Attention is currently required from: flichtenheld, plaisthos.

cron2 has posted comments on this change by flichtenheld. ( http://gerrit.openvpn.net/c/openvpn/+/1520?usp=email )

Change subject: DNS/DHCP: Make _len attributes consistently unsigned
......................................................................

Patch Set 4: Code-Review+2

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/1520?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings?usp=email

Gerrit-MessageType: comment
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Id2c5df9df32a02e944e13b55f57a2c1928b652f4
Gerrit-Change-Number: 1520
Gerrit-PatchSet: 4
Gerrit-Owner: flichtenheld <[email protected]...>
Gerrit-Reviewer: cron2 <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-Attention: flichtenheld <[email protected]...>
Gerrit-Comment-Date: Wed, 11 Mar 2026 19:58:59 +0000
Gerrit-HasComments: No
Gerrit-Has-Labels: Yes

#3 lore raw

PATCH v4 1/1 DNS/DHCP: Make _len attributes consistently unsigned

In reply to a message not in the archive

From: Frank Lichtenheld <[email protected]...>

Use unsigned int instead of size_t since 32bit
values are quite enough here and this avoids some
unneccesary casts.

Change-Id Id2c5df9df32a02e944e13b55f57a2c1928b652f4

Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1520
---

This change was reviewed on Gerrit and approved by at least one developer. I request to merge it to master.

Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1520
This mail reflects revision 4 of this Change.

Acked-by according to Gerrit (reflected above):
Gert Doering <[email protected]...>

include/openvpn-msg.h +2 -2
index 34f50c8..bef20cf 100644
@@ -103,7 +103,7 @@
     interface_t iface;
     char domains[512];
     short family;
-    int addr_len;
+    unsigned int addr_len;
     inet_address_t addr[4]; /* support up to 4 dns addresses */
 } dns_cfg_message_t;
 
@@ -130,7 +130,7 @@
 {
     message_header_t header;
     interface_t iface;
-    int addr_len;
+    unsigned int addr_len;
     inet_address_t addr[4]; /* support up to 4 dns addresses */
 } wins_cfg_message_t;
 
src/openvpn/dns.h +3 -3
index 882e474..51bc2de 100644
@@ -99,14 +99,14 @@
 struct dhcp_options
 {
     in_addr_t dns[N_DHCP_ADDR];
-    int dns_len;
+    unsigned int dns_len;
 
     struct in6_addr dns6[N_DHCP_ADDR];
-    int dns6_len;
+    unsigned int dns6_len;
 
     const char *domain;
     const char *domain_search_list[N_SEARCH_LIST_LEN];
-    int domain_search_list_len;
+    unsigned int domain_search_list_len;
 };
 
 struct dns_options
src/openvpn/options.c +13 -15
index 8daec42..e065e63 100644
@@ -1258,23 +1258,21 @@
 #ifndef ENABLE_SMALL
 
 static void
-show_dhcp_option_list(const char *name, const char *const *array, int len)
+show_dhcp_option_list(const char *name, const char *const *array, unsigned int len)
 {
-    int i;
-    for (i = 0; i < len; ++i)
+    for (unsigned int i = 0; i < len; ++i)
     {
-        msg(D_SHOW_PARMS, "  %s[%d] = %s", name, i, array[i]);
+        msg(D_SHOW_PARMS, "  %s[%u] = %s", name, i, array[i]);
     }
 }
 
 static void
-show_dhcp_option_addrs(const char *name, const in_addr_t *array, int len)
+show_dhcp_option_addrs(const char *name, const in_addr_t *array, unsigned int len)
 {
     struct gc_arena gc = gc_new();
-    int i;
-    for (i = 0; i < len; ++i)
+    for (unsigned int i = 0; i < len; ++i)
     {
-        msg(D_SHOW_PARMS, "  %s[%d] = %s", name, i, print_in_addr_t(array[i], 0, &gc));
+        msg(D_SHOW_PARMS, "  %s[%u] = %s", name, i, print_in_addr_t(array[i], 0, &gc));
     }
     gc_free(&gc);
 }
@@ -1306,12 +1304,12 @@
 #endif /* ifdef _WIN32 */
 
 static void
-dhcp_option_dns6_parse(const char *parm, struct in6_addr *dns6_list, int *len, msglvl_t msglevel)
+dhcp_option_dns6_parse(const char *parm, struct in6_addr *dns6_list, unsigned int *len, msglvl_t msglevel)
 {
     struct in6_addr addr;
     if (*len >= N_DHCP_ADDR)
     {
-        msg(msglevel, "--dhcp-option DNS: maximum of %d IPv6 dns servers can be specified",
+        msg(msglevel, "--dhcp-option DNS: maximum of %u IPv6 dns servers can be specified",
             N_DHCP_ADDR);
     }
     else if (get_ipv6_addr(parm, &addr, NULL, msglevel))
@@ -1320,12 +1318,12 @@
     }
 }
 static void
-dhcp_option_address_parse(const char *name, const char *parm, in_addr_t *array, int *len,
+dhcp_option_address_parse(const char *name, const char *parm, in_addr_t *array, unsigned int *len,
                           msglvl_t msglevel)
 {
     if (*len >= N_DHCP_ADDR)
     {
-        msg(msglevel, "--dhcp-option %s: maximum of %d %s servers can be specified", name,
+        msg(msglevel, "--dhcp-option %s: maximum of %u %s servers can be specified", name,
             N_DHCP_ADDR, name);
     }
     else
@@ -3648,7 +3646,7 @@
             new->name = dhcp->domain;
             entry = &new->next;
 
-            for (int i = 0; i < dhcp->domain_search_list_len; ++i)
+            for (unsigned int i = 0; i < dhcp->domain_search_list_len; ++i)
             {
                 ALLOC_OBJ_CLEAR_GC(*entry, struct dns_domain, &dns->gc);
                 struct dns_domain *new = *entry;
@@ -3658,13 +3656,13 @@
 
             struct dns_server *server = dns_server_get(&dns->servers, 0, &dns->gc);
             const size_t max_addrs = SIZE(server->addr);
-            for (int i = 0; i < dhcp->dns_len && server->addr_count < max_addrs; ++i)
+            for (unsigned int i = 0; i < dhcp->dns_len && server->addr_count < max_addrs; ++i)
             {
                 server->addr[server->addr_count].in.a4.s_addr = htonl(dhcp->dns[i]);
                 server->addr[server->addr_count].family = AF_INET;
                 server->addr_count += 1;
             }
-            for (int i = 0; i < dhcp->dns6_len && server->addr_count < max_addrs; ++i)
+            for (unsigned int i = 0; i < dhcp->dns6_len && server->addr_count < max_addrs; ++i)
             {
                 server->addr[server->addr_count].in.a6 = dhcp->dns6[i];
                 server->addr[server->addr_count].family = AF_INET6;
src/openvpn/tun.c +32 -42

204 lines not on this page · raw message

src/openvpn/tun.h +6 -6
index ecb5e88..6a7af85 100644
@@ -109,25 +109,25 @@
 
     /* DNS (6) */
     in_addr_t dns[N_DHCP_ADDR];
-    int dns_len;
+    unsigned int dns_len;
 
     /* WINS (44) */
     in_addr_t wins[N_DHCP_ADDR];
-    int wins_len;
+    unsigned int wins_len;
 
     /* NTP (42) */
     in_addr_t ntp[N_DHCP_ADDR];
-    int ntp_len;
+    unsigned int ntp_len;
 
     /* NBDD (45) */
     in_addr_t nbdd[N_DHCP_ADDR];
-    int nbdd_len;
+    unsigned int nbdd_len;
 
 #define N_SEARCH_LIST_LEN 10 /* Max # of entries in domin-search list */
 
     /* SEARCH (119), MacOS, Linux, Win10 1809+ */
     const char *domain_search_list[N_SEARCH_LIST_LEN];
-    int domain_search_list_len;
+    unsigned int domain_search_list_len;
 
     /* DISABLE_NBT (43, Vendor option 001) */
     bool disable_nbt;
@@ -138,7 +138,7 @@
     bool register_dns;
 
     struct in6_addr dns6[N_DHCP_ADDR];
-    int dns6_len;
+    unsigned int dns6_len;
 #if defined(TARGET_ANDROID)
     const char *http_proxy;
     int http_proxy_port;
src/openvpnserv/interactive.c +6 -10
index b50aeb3..c96d3f2 100644
@@ -1814,10 +1814,10 @@
 {
     DWORD err = 0;
     undo_type_t undo_type = (msg->family == AF_INET6) ? undo_dns6 : undo_dns4;
-    int addr_len = msg->addr_len;
+    unsigned int addr_len = msg->addr_len;
 
     /* sanity check */
-    const int max_addrs = _countof(msg->addr);
+    const unsigned int max_addrs = _countof(msg->addr);
     if (addr_len > max_addrs)
     {
         addr_len = max_addrs;
@@ -1875,7 +1875,7 @@
          * time constant by all compilers and constexpr is C23 */
         CHAR addrs[_countof(msg->addr) * 64]; /* 64 is enough for one IPv4/6 address */
         size_t offset = 0;
-        for (int i = 0; i < addr_len; ++i)
+        for (unsigned int i = 0; i < addr_len; ++i)
         {
             if (i != 0)
             {
@@ -2936,14 +2936,10 @@
 {
     DWORD err = NO_ERROR;
     wchar_t addr[16]; /* large enough to hold string representation of an ipv4 */
-    int addr_len = msg->addr_len;
+    unsigned int addr_len = msg->addr_len;
 
     /* sanity check */
-    if (addr_len < 0)
-    {
-        addr_len = 0;
-    }
-    if ((unsigned int)addr_len > _countof(msg->addr))
+    if (addr_len > _countof(msg->addr))
     {
         addr_len = _countof(msg->addr);
     }
@@ -2971,7 +2967,7 @@
         goto out; /* job done */
     }
 
-    for (int i = 0; i < addr_len; ++i)
+    for (unsigned int i = 0; i < addr_len; ++i)
     {
         RtlIpv4AddressToStringW(&msg->addr[i].ipv4, addr);
         err = netsh_wins_cmd(i == 0 ? L"set" : L"add", msg->iface.index, addr);
#4 lore raw

[Openvpn-devel] [PATCH applied] Re: DNS/DHCP: Make _len attributes consistently unsigned

Quite a few changes, but it all makes sense. BB happy :-).

Compile-tested on Ubuntu 24.04 / mingw64. Did not actually test the resulting binaries.

Your patch has been applied to the master branch.

commit 28a7bd69953af35f976a4005835523a65e13e558

Author: Frank Lichtenheld
Date:   Wed Mar 11 20:59:20 2026 +0100
     DNS/DHCP: Make _len attributes consistently unsigned

     Signed-off-by: Frank Lichtenheld <[email protected]...>
     Acked-by: Gert Doering <[email protected]...>
     Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1520
     Message-Id: <[email protected]...>
     URL: https://www.mail-archive.com/[email protected]/msg36038.html
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Gert Doering

#5 lore raw
In reply to a message not in the archive

cron2 has uploaded a new patch set (#5) to the change originally created by flichtenheld. ( http://gerrit.openvpn.net/c/openvpn/+/1520?usp=email )

The following approvals got outdated and were removed:
Code-Review+2 by cron2

Change subject: DNS/DHCP: Make _len attributes consistently unsigned
......................................................................

DNS/DHCP: Make _len attributes consistently unsigned

Use unsigned int instead of size_t since 32bit
values are quite enough here and this avoids some
unneccesary casts.

Change-Id Id2c5df9df32a02e944e13b55f57a2c1928b652f4

Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1520
Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/[email protected]/msg36038.html

---
M include/openvpn-msg.h
M src/openvpn/dns.h
M src/openvpn/options.c
M src/openvpn/tun.c
M src/openvpn/tun.h
M src/openvpnserv/interactive.c

6 files changed, 62 insertions(+), 78 deletions(-)

git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/20/1520/5

include/openvpn-msg.h +2 -2
index 34f50c8..bef20cf 100644
@@ -103,7 +103,7 @@
     interface_t iface;
     char domains[512];
     short family;
-    int addr_len;
+    unsigned int addr_len;
     inet_address_t addr[4]; /* support up to 4 dns addresses */
 } dns_cfg_message_t;
 
@@ -130,7 +130,7 @@
 {
     message_header_t header;
     interface_t iface;
-    int addr_len;
+    unsigned int addr_len;
     inet_address_t addr[4]; /* support up to 4 dns addresses */
 } wins_cfg_message_t;
 
src/openvpn/dns.h +3 -3
index 882e474..51bc2de 100644
@@ -99,14 +99,14 @@
 struct dhcp_options
 {
     in_addr_t dns[N_DHCP_ADDR];
-    int dns_len;
+    unsigned int dns_len;
 
     struct in6_addr dns6[N_DHCP_ADDR];
-    int dns6_len;
+    unsigned int dns6_len;
 
     const char *domain;
     const char *domain_search_list[N_SEARCH_LIST_LEN];
-    int domain_search_list_len;
+    unsigned int domain_search_list_len;
 };
 
 struct dns_options
src/openvpn/options.c +13 -15
index 8daec42..e065e63 100644
@@ -1258,23 +1258,21 @@
 #ifndef ENABLE_SMALL
 
 static void
-show_dhcp_option_list(const char *name, const char *const *array, int len)
+show_dhcp_option_list(const char *name, const char *const *array, unsigned int len)
 {
-    int i;
-    for (i = 0; i < len; ++i)
+    for (unsigned int i = 0; i < len; ++i)
     {
-        msg(D_SHOW_PARMS, "  %s[%d] = %s", name, i, array[i]);
+        msg(D_SHOW_PARMS, "  %s[%u] = %s", name, i, array[i]);
     }
 }
 
 static void
-show_dhcp_option_addrs(const char *name, const in_addr_t *array, int len)
+show_dhcp_option_addrs(const char *name, const in_addr_t *array, unsigned int len)
 {
     struct gc_arena gc = gc_new();
-    int i;
-    for (i = 0; i < len; ++i)
+    for (unsigned int i = 0; i < len; ++i)
     {
-        msg(D_SHOW_PARMS, "  %s[%d] = %s", name, i, print_in_addr_t(array[i], 0, &gc));
+        msg(D_SHOW_PARMS, "  %s[%u] = %s", name, i, print_in_addr_t(array[i], 0, &gc));
     }
     gc_free(&gc);
 }
@@ -1306,12 +1304,12 @@
 #endif /* ifdef _WIN32 */
 
 static void
-dhcp_option_dns6_parse(const char *parm, struct in6_addr *dns6_list, int *len, msglvl_t msglevel)
+dhcp_option_dns6_parse(const char *parm, struct in6_addr *dns6_list, unsigned int *len, msglvl_t msglevel)
 {
     struct in6_addr addr;
     if (*len >= N_DHCP_ADDR)
     {
-        msg(msglevel, "--dhcp-option DNS: maximum of %d IPv6 dns servers can be specified",
+        msg(msglevel, "--dhcp-option DNS: maximum of %u IPv6 dns servers can be specified",
             N_DHCP_ADDR);
     }
     else if (get_ipv6_addr(parm, &addr, NULL, msglevel))
@@ -1320,12 +1318,12 @@
     }
 }
 static void
-dhcp_option_address_parse(const char *name, const char *parm, in_addr_t *array, int *len,
+dhcp_option_address_parse(const char *name, const char *parm, in_addr_t *array, unsigned int *len,
                           msglvl_t msglevel)
 {
     if (*len >= N_DHCP_ADDR)
     {
-        msg(msglevel, "--dhcp-option %s: maximum of %d %s servers can be specified", name,
+        msg(msglevel, "--dhcp-option %s: maximum of %u %s servers can be specified", name,
             N_DHCP_ADDR, name);
     }
     else
@@ -3648,7 +3646,7 @@
             new->name = dhcp->domain;
             entry = &new->next;
 
-            for (int i = 0; i < dhcp->domain_search_list_len; ++i)
+            for (unsigned int i = 0; i < dhcp->domain_search_list_len; ++i)
             {
                 ALLOC_OBJ_CLEAR_GC(*entry, struct dns_domain, &dns->gc);
                 struct dns_domain *new = *entry;
@@ -3658,13 +3656,13 @@
 
             struct dns_server *server = dns_server_get(&dns->servers, 0, &dns->gc);
             const size_t max_addrs = SIZE(server->addr);
-            for (int i = 0; i < dhcp->dns_len && server->addr_count < max_addrs; ++i)
+            for (unsigned int i = 0; i < dhcp->dns_len && server->addr_count < max_addrs; ++i)
             {
                 server->addr[server->addr_count].in.a4.s_addr = htonl(dhcp->dns[i]);
                 server->addr[server->addr_count].family = AF_INET;
                 server->addr_count += 1;
             }
-            for (int i = 0; i < dhcp->dns6_len && server->addr_count < max_addrs; ++i)
+            for (unsigned int i = 0; i < dhcp->dns6_len && server->addr_count < max_addrs; ++i)
             {
                 server->addr[server->addr_count].in.a6 = dhcp->dns6[i];
                 server->addr[server->addr_count].family = AF_INET6;
src/openvpn/tun.c +32 -42

204 lines not on this page · raw message

src/openvpn/tun.h +6 -6
index ecb5e88..6a7af85 100644
@@ -109,25 +109,25 @@
 
     /* DNS (6) */
     in_addr_t dns[N_DHCP_ADDR];
-    int dns_len;
+    unsigned int dns_len;
 
     /* WINS (44) */
     in_addr_t wins[N_DHCP_ADDR];
-    int wins_len;
+    unsigned int wins_len;
 
     /* NTP (42) */
     in_addr_t ntp[N_DHCP_ADDR];
-    int ntp_len;
+    unsigned int ntp_len;
 
     /* NBDD (45) */
     in_addr_t nbdd[N_DHCP_ADDR];
-    int nbdd_len;
+    unsigned int nbdd_len;
 
 #define N_SEARCH_LIST_LEN 10 /* Max # of entries in domin-search list */
 
     /* SEARCH (119), MacOS, Linux, Win10 1809+ */
     const char *domain_search_list[N_SEARCH_LIST_LEN];
-    int domain_search_list_len;
+    unsigned int domain_search_list_len;
 
     /* DISABLE_NBT (43, Vendor option 001) */
     bool disable_nbt;
@@ -138,7 +138,7 @@
     bool register_dns;
 
     struct in6_addr dns6[N_DHCP_ADDR];
-    int dns6_len;
+    unsigned int dns6_len;
 #if defined(TARGET_ANDROID)
     const char *http_proxy;
     int http_proxy_port;
src/openvpnserv/interactive.c +6 -10
index b50aeb3..c96d3f2 100644
@@ -1814,10 +1814,10 @@
 {
     DWORD err = 0;
     undo_type_t undo_type = (msg->family == AF_INET6) ? undo_dns6 : undo_dns4;
-    int addr_len = msg->addr_len;
+    unsigned int addr_len = msg->addr_len;
 
     /* sanity check */
-    const int max_addrs = _countof(msg->addr);
+    const unsigned int max_addrs = _countof(msg->addr);
     if (addr_len > max_addrs)
     {
         addr_len = max_addrs;
@@ -1875,7 +1875,7 @@
          * time constant by all compilers and constexpr is C23 */
         CHAR addrs[_countof(msg->addr) * 64]; /* 64 is enough for one IPv4/6 address */
         size_t offset = 0;
-        for (int i = 0; i < addr_len; ++i)
+        for (unsigned int i = 0; i < addr_len; ++i)
         {
             if (i != 0)
             {
@@ -2936,14 +2936,10 @@
 {
     DWORD err = NO_ERROR;
     wchar_t addr[16]; /* large enough to hold string representation of an ipv4 */
-    int addr_len = msg->addr_len;
+    unsigned int addr_len = msg->addr_len;
 
     /* sanity check */
-    if (addr_len < 0)
-    {
-        addr_len = 0;
-    }
-    if ((unsigned int)addr_len > _countof(msg->addr))
+    if (addr_len > _countof(msg->addr))
     {
         addr_len = _countof(msg->addr);
     }
@@ -2971,7 +2967,7 @@
         goto out; /* job done */
     }
 
-    for (int i = 0; i < addr_len; ++i)
+    for (unsigned int i = 0; i < addr_len; ++i)
     {
         RtlIpv4AddressToStringW(&msg->addr[i].ipv4, addr);
         err = netsh_wins_cmd(i == 0 ? L"set" : L"add", msg->iface.index, addr);

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/1520?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings?usp=email

Gerrit-MessageType: newpatchset
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Id2c5df9df32a02e944e13b55f57a2c1928b652f4
Gerrit-Change-Number: 1520
Gerrit-PatchSet: 5
Gerrit-Owner: flichtenheld <[email protected]...>
Gerrit-Reviewer: cron2 <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>

#6 lore raw
In reply to a message not in the archive

cron2 has submitted this change. ( http://gerrit.openvpn.net/c/openvpn/+/1520?usp=email )

Change subject: DNS/DHCP: Make _len attributes consistently unsigned
......................................................................

DNS/DHCP: Make _len attributes consistently unsigned

Use unsigned int instead of size_t since 32bit
values are quite enough here and this avoids some
unneccesary casts.

Change-Id Id2c5df9df32a02e944e13b55f57a2c1928b652f4

Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1520
Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/[email protected]/msg36038.html

---
M include/openvpn-msg.h
M src/openvpn/dns.h
M src/openvpn/options.c
M src/openvpn/tun.c
M src/openvpn/tun.h
M src/openvpnserv/interactive.c

6 files changed, 62 insertions(+), 78 deletions(-)
include/openvpn-msg.h +2 -2
index 34f50c8..bef20cf 100644
@@ -103,7 +103,7 @@
     interface_t iface;
     char domains[512];
     short family;
-    int addr_len;
+    unsigned int addr_len;
     inet_address_t addr[4]; /* support up to 4 dns addresses */
 } dns_cfg_message_t;
 
@@ -130,7 +130,7 @@
 {
     message_header_t header;
     interface_t iface;
-    int addr_len;
+    unsigned int addr_len;
     inet_address_t addr[4]; /* support up to 4 dns addresses */
 } wins_cfg_message_t;
 
src/openvpn/dns.h +3 -3
index 882e474..51bc2de 100644
@@ -99,14 +99,14 @@
 struct dhcp_options
 {
     in_addr_t dns[N_DHCP_ADDR];
-    int dns_len;
+    unsigned int dns_len;
 
     struct in6_addr dns6[N_DHCP_ADDR];
-    int dns6_len;
+    unsigned int dns6_len;
 
     const char *domain;
     const char *domain_search_list[N_SEARCH_LIST_LEN];
-    int domain_search_list_len;
+    unsigned int domain_search_list_len;
 };
 
 struct dns_options
src/openvpn/options.c +13 -15
index 8daec42..e065e63 100644
@@ -1258,23 +1258,21 @@
 #ifndef ENABLE_SMALL
 
 static void
-show_dhcp_option_list(const char *name, const char *const *array, int len)
+show_dhcp_option_list(const char *name, const char *const *array, unsigned int len)
 {
-    int i;
-    for (i = 0; i < len; ++i)
+    for (unsigned int i = 0; i < len; ++i)
     {
-        msg(D_SHOW_PARMS, "  %s[%d] = %s", name, i, array[i]);
+        msg(D_SHOW_PARMS, "  %s[%u] = %s", name, i, array[i]);
     }
 }
 
 static void
-show_dhcp_option_addrs(const char *name, const in_addr_t *array, int len)
+show_dhcp_option_addrs(const char *name, const in_addr_t *array, unsigned int len)
 {
     struct gc_arena gc = gc_new();
-    int i;
-    for (i = 0; i < len; ++i)
+    for (unsigned int i = 0; i < len; ++i)
     {
-        msg(D_SHOW_PARMS, "  %s[%d] = %s", name, i, print_in_addr_t(array[i], 0, &gc));
+        msg(D_SHOW_PARMS, "  %s[%u] = %s", name, i, print_in_addr_t(array[i], 0, &gc));
     }
     gc_free(&gc);
 }
@@ -1306,12 +1304,12 @@
 #endif /* ifdef _WIN32 */
 
 static void
-dhcp_option_dns6_parse(const char *parm, struct in6_addr *dns6_list, int *len, msglvl_t msglevel)
+dhcp_option_dns6_parse(const char *parm, struct in6_addr *dns6_list, unsigned int *len, msglvl_t msglevel)
 {
     struct in6_addr addr;
     if (*len >= N_DHCP_ADDR)
     {
-        msg(msglevel, "--dhcp-option DNS: maximum of %d IPv6 dns servers can be specified",
+        msg(msglevel, "--dhcp-option DNS: maximum of %u IPv6 dns servers can be specified",
             N_DHCP_ADDR);
     }
     else if (get_ipv6_addr(parm, &addr, NULL, msglevel))
@@ -1320,12 +1318,12 @@
     }
 }
 static void
-dhcp_option_address_parse(const char *name, const char *parm, in_addr_t *array, int *len,
+dhcp_option_address_parse(const char *name, const char *parm, in_addr_t *array, unsigned int *len,
                           msglvl_t msglevel)
 {
     if (*len >= N_DHCP_ADDR)
     {
-        msg(msglevel, "--dhcp-option %s: maximum of %d %s servers can be specified", name,
+        msg(msglevel, "--dhcp-option %s: maximum of %u %s servers can be specified", name,
             N_DHCP_ADDR, name);
     }
     else
@@ -3648,7 +3646,7 @@
             new->name = dhcp->domain;
             entry = &new->next;
 
-            for (int i = 0; i < dhcp->domain_search_list_len; ++i)
+            for (unsigned int i = 0; i < dhcp->domain_search_list_len; ++i)
             {
                 ALLOC_OBJ_CLEAR_GC(*entry, struct dns_domain, &dns->gc);
                 struct dns_domain *new = *entry;
@@ -3658,13 +3656,13 @@
 
             struct dns_server *server = dns_server_get(&dns->servers, 0, &dns->gc);
             const size_t max_addrs = SIZE(server->addr);
-            for (int i = 0; i < dhcp->dns_len && server->addr_count < max_addrs; ++i)
+            for (unsigned int i = 0; i < dhcp->dns_len && server->addr_count < max_addrs; ++i)
             {
                 server->addr[server->addr_count].in.a4.s_addr = htonl(dhcp->dns[i]);
                 server->addr[server->addr_count].family = AF_INET;
                 server->addr_count += 1;
             }
-            for (int i = 0; i < dhcp->dns6_len && server->addr_count < max_addrs; ++i)
+            for (unsigned int i = 0; i < dhcp->dns6_len && server->addr_count < max_addrs; ++i)
             {
                 server->addr[server->addr_count].in.a6 = dhcp->dns6[i];
                 server->addr[server->addr_count].family = AF_INET6;
src/openvpn/tun.c +32 -42

204 lines not on this page · raw message

src/openvpn/tun.h +6 -6
index ecb5e88..6a7af85 100644
@@ -109,25 +109,25 @@
 
     /* DNS (6) */
     in_addr_t dns[N_DHCP_ADDR];
-    int dns_len;
+    unsigned int dns_len;
 
     /* WINS (44) */
     in_addr_t wins[N_DHCP_ADDR];
-    int wins_len;
+    unsigned int wins_len;
 
     /* NTP (42) */
     in_addr_t ntp[N_DHCP_ADDR];
-    int ntp_len;
+    unsigned int ntp_len;
 
     /* NBDD (45) */
     in_addr_t nbdd[N_DHCP_ADDR];
-    int nbdd_len;
+    unsigned int nbdd_len;
 
 #define N_SEARCH_LIST_LEN 10 /* Max # of entries in domin-search list */
 
     /* SEARCH (119), MacOS, Linux, Win10 1809+ */
     const char *domain_search_list[N_SEARCH_LIST_LEN];
-    int domain_search_list_len;
+    unsigned int domain_search_list_len;
 
     /* DISABLE_NBT (43, Vendor option 001) */
     bool disable_nbt;
@@ -138,7 +138,7 @@
     bool register_dns;
 
     struct in6_addr dns6[N_DHCP_ADDR];
-    int dns6_len;
+    unsigned int dns6_len;
 #if defined(TARGET_ANDROID)
     const char *http_proxy;
     int http_proxy_port;
src/openvpnserv/interactive.c +6 -10
index b50aeb3..c96d3f2 100644
@@ -1814,10 +1814,10 @@
 {
     DWORD err = 0;
     undo_type_t undo_type = (msg->family == AF_INET6) ? undo_dns6 : undo_dns4;
-    int addr_len = msg->addr_len;
+    unsigned int addr_len = msg->addr_len;
 
     /* sanity check */
-    const int max_addrs = _countof(msg->addr);
+    const unsigned int max_addrs = _countof(msg->addr);
     if (addr_len > max_addrs)
     {
         addr_len = max_addrs;
@@ -1875,7 +1875,7 @@
          * time constant by all compilers and constexpr is C23 */
         CHAR addrs[_countof(msg->addr) * 64]; /* 64 is enough for one IPv4/6 address */
         size_t offset = 0;
-        for (int i = 0; i < addr_len; ++i)
+        for (unsigned int i = 0; i < addr_len; ++i)
         {
             if (i != 0)
             {
@@ -2936,14 +2936,10 @@
 {
     DWORD err = NO_ERROR;
     wchar_t addr[16]; /* large enough to hold string representation of an ipv4 */
-    int addr_len = msg->addr_len;
+    unsigned int addr_len = msg->addr_len;
 
     /* sanity check */
-    if (addr_len < 0)
-    {
-        addr_len = 0;
-    }
-    if ((unsigned int)addr_len > _countof(msg->addr))
+    if (addr_len > _countof(msg->addr))
     {
         addr_len = _countof(msg->addr);
     }
@@ -2971,7 +2967,7 @@
         goto out; /* job done */
     }
 
-    for (int i = 0; i < addr_len; ++i)
+    for (unsigned int i = 0; i < addr_len; ++i)
     {
         RtlIpv4AddressToStringW(&msg->addr[i].ipv4, addr);
         err = netsh_wins_cmd(i == 0 ? L"set" : L"add", msg->iface.index, addr);

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/1520?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings?usp=email

Gerrit-MessageType: merged
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Id2c5df9df32a02e944e13b55f57a2c1928b652f4
Gerrit-Change-Number: 1520
Gerrit-PatchSet: 5
Gerrit-Owner: flichtenheld <[email protected]...>
Gerrit-Reviewer: cron2 <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>